home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 April / macformat-049.iso / mac / Shareware Plus / Developers / dropg++ / usr / include / sys / malloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  10.9 KB  |  307 lines  |  [TEXT/R*ch]

  1. /*
  2.  * Copyright (c) 1987, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  *    @(#)malloc.h    8.3 (Berkeley) 1/12/94
  34.  */
  35.  
  36. #ifndef _SYS_MALLOC_H_
  37. #define    _SYS_MALLOC_H_
  38.  
  39. #define KMEMSTATS
  40.  
  41. /*
  42.  * flags to malloc
  43.  */
  44. #define    M_WAITOK    0x0000
  45. #define    M_NOWAIT    0x0001
  46.  
  47. /*
  48.  * Types of memory to be allocated
  49.  */
  50. #define    M_FREE        0    /* should be on free list */
  51. #define    M_MBUF        1    /* mbuf */
  52. #define    M_DEVBUF    2    /* device driver memory */
  53. #define    M_SOCKET    3    /* socket structure */
  54. #define    M_PCB        4    /* protocol control block */
  55. #define    M_RTABLE    5    /* routing tables */
  56. #define    M_HTABLE    6    /* IMP host tables */
  57. #define    M_FTABLE    7    /* fragment reassembly header */
  58. #define    M_ZOMBIE    8    /* zombie proc status */
  59. #define    M_IFADDR    9    /* interface address */
  60. #define    M_SOOPTS    10    /* socket options */
  61. #define    M_SONAME    11    /* socket name */
  62. #define    M_NAMEI        12    /* namei path name buffer */
  63. #define    M_GPROF        13    /* kernel profiling buffer */
  64. #define    M_IOCTLOPS    14    /* ioctl data buffer */
  65. #define    M_MAPMEM    15    /* mapped memory descriptors */
  66. #define    M_CRED        16    /* credentials */
  67. #define    M_PGRP        17    /* process group header */
  68. #define    M_SESSION    18    /* session header */
  69. #define    M_IOV        19    /* large iov's */
  70. #define    M_MOUNT        20    /* vfs mount struct */
  71. #define    M_FHANDLE    21    /* network file handle */
  72. #define    M_NFSREQ    22    /* NFS request header */
  73. #define    M_NFSMNT    23    /* NFS mount structure */
  74. #define    M_NFSNODE    24    /* NFS vnode private part */
  75. #define    M_VNODE        25    /* Dynamically allocated vnodes */
  76. #define    M_CACHE        26    /* Dynamically allocated cache entries */
  77. #define    M_DQUOT        27    /* UFS quota entries */
  78. #define    M_UFSMNT    28    /* UFS mount structure */
  79. #define    M_SHM        29    /* SVID compatible shared memory segments */
  80. #define    M_VMMAP        30    /* VM map structures */
  81. #define    M_VMMAPENT    31    /* VM map entry structures */
  82. #define    M_VMOBJ        32    /* VM object structure */
  83. #define    M_VMOBJHASH    33    /* VM object hash structure */
  84. #define    M_VMPMAP    34    /* VM pmap */
  85. #define    M_VMPVENT    35    /* VM phys-virt mapping entry */
  86. #define    M_VMPAGER    36    /* XXX: VM pager struct */
  87. #define    M_VMPGDATA    37    /* XXX: VM pager private data */
  88. #define    M_FILE        38    /* Open file structure */
  89. #define    M_FILEDESC    39    /* Open file descriptor table */
  90. #define    M_LOCKF        40    /* Byte-range locking structures */
  91. #define    M_PROC        41    /* Proc structures */
  92. #define    M_SUBPROC    42    /* Proc sub-structures */
  93. #define    M_SEGMENT    43    /* Segment for LFS */
  94. #define    M_LFSNODE    44    /* LFS vnode private part */
  95. #define    M_FFSNODE    45    /* FFS vnode private part */
  96. #define    M_MFSNODE    46    /* MFS vnode private part */
  97. #define    M_NQLEASE    47    /* Nqnfs lease */
  98. #define    M_NQMHOST    48    /* Nqnfs host address table */
  99. #define    M_NETADDR    49    /* Export host address structure */
  100. #define    M_NFSSVC    50    /* Nfs server structure */
  101. #define    M_NFSUID    51    /* Nfs uid mapping structure */
  102. #define    M_NFSD        52    /* Nfs server daemon structure */
  103. #define    M_IPMOPTS    53    /* internet multicast options */
  104. #define    M_IPMADDR    54    /* internet multicast address */
  105. #define    M_IFMADDR    55    /* link-level multicast address */
  106. #define    M_MRTABLE    56    /* multicast routing tables */
  107. #define M_ISOFSMNT    57    /* ISOFS mount structure */
  108. #define M_ISOFSNODE    58    /* ISOFS vnode private part */
  109. #define    M_TEMP        74    /* misc temporary data buffers */
  110. #define    M_LAST        75    /* Must be last type + 1 */
  111.  
  112. #define INITKMEMNAMES { \
  113.     "free",        /* 0 M_FREE */ \
  114.     "mbuf",        /* 1 M_MBUF */ \
  115.     "devbuf",    /* 2 M_DEVBUF */ \
  116.     "socket",    /* 3 M_SOCKET */ \
  117.     "pcb",        /* 4 M_PCB */ \
  118.     "routetbl",    /* 5 M_RTABLE */ \
  119.     "hosttbl",    /* 6 M_HTABLE */ \
  120.     "fragtbl",    /* 7 M_FTABLE */ \
  121.     "zombie",    /* 8 M_ZOMBIE */ \
  122.     "ifaddr",    /* 9 M_IFADDR */ \
  123.     "soopts",    /* 10 M_SOOPTS */ \
  124.     "soname",    /* 11 M_SONAME */ \
  125.     "namei",    /* 12 M_NAMEI */ \
  126.     "gprof",    /* 13 M_GPROF */ \
  127.     "ioctlops",    /* 14 M_IOCTLOPS */ \
  128.     "mapmem",    /* 15 M_MAPMEM */ \
  129.     "cred",        /* 16 M_CRED */ \
  130.     "pgrp",        /* 17 M_PGRP */ \
  131.     "session",    /* 18 M_SESSION */ \
  132.     "iov",        /* 19 M_IOV */ \
  133.     "mount",    /* 20 M_MOUNT */ \
  134.     "fhandle",    /* 21 M_FHANDLE */ \
  135.     "NFS req",    /* 22 M_NFSREQ */ \
  136.     "NFS mount",    /* 23 M_NFSMNT */ \
  137.     "NFS node",    /* 24 M_NFSNODE */ \
  138.     "vnodes",    /* 25 M_VNODE */ \
  139.     "namecache",    /* 26 M_CACHE */ \
  140.     "UFS quota",    /* 27 M_DQUOT */ \
  141.     "UFS mount",    /* 28 M_UFSMNT */ \
  142.     "shm",        /* 29 M_SHM */ \
  143.     "VM map",    /* 30 M_VMMAP */ \
  144.     "VM mapent",    /* 31 M_VMMAPENT */ \
  145.     "VM object",    /* 32 M_VMOBJ */ \
  146.     "VM objhash",    /* 33 M_VMOBJHASH */ \
  147.     "VM pmap",    /* 34 M_VMPMAP */ \
  148.     "VM pvmap",    /* 35 M_VMPVENT */ \
  149.     "VM pager",    /* 36 M_VMPAGER */ \
  150.     "VM pgdata",    /* 37 M_VMPGDATA */ \
  151.     "file",        /* 38 M_FILE */ \
  152.     "file desc",    /* 39 M_FILEDESC */ \
  153.     "lockf",    /* 40 M_LOCKF */ \
  154.     "proc",        /* 41 M_PROC */ \
  155.     "subproc",    /* 42 M_SUBPROC */ \
  156.     "LFS segment",    /* 43 M_SEGMENT */ \
  157.     "LFS node",    /* 44 M_LFSNODE */ \
  158.     "FFS node",    /* 45 M_FFSNODE */ \
  159.     "MFS node",    /* 46 M_MFSNODE */ \
  160.     "NQNFS Lease",    /* 47 M_NQLEASE */ \
  161.     "NQNFS Host",    /* 48 M_NQMHOST */ \
  162.     "Export Host",    /* 49 M_NETADDR */ \
  163.     "NFS srvsock",    /* 50 M_NFSSVC */ \
  164.     "NFS uid",    /* 51 M_NFSUID */ \
  165.     "NFS daemon",    /* 52 M_NFSD */ \
  166.     "ip_moptions",    /* 53 M_IPMOPTS */ \
  167.     "in_multi",    /* 54 M_IPMADDR */ \
  168.     "ether_multi",    /* 55 M_IFMADDR */ \
  169.     "mrt",        /* 56 M_MRTABLE */ \
  170.     "ISOFS mount",    /* 57 M_ISOFSMNT */ \
  171.     "ISOFS node",    /* 58 M_ISOFSNODE */ \
  172.     NULL, NULL, NULL, NULL, NULL, \
  173.     NULL, NULL, NULL, NULL, NULL, \
  174.     NULL, NULL, NULL, NULL, NULL, \
  175.     "temp",        /* 74 M_TEMP */ \
  176. }
  177.  
  178. struct kmemstats {
  179.     long    ks_inuse;    /* # of packets of this type currently in use */
  180.     long    ks_calls;    /* total packets of this type ever allocated */
  181.     long     ks_memuse;    /* total memory held in bytes */
  182.     u_short    ks_limblocks;    /* number of times blocked for hitting limit */
  183.     u_short    ks_mapblocks;    /* number of times blocked for kernel map */
  184.     long    ks_maxused;    /* maximum number ever used */
  185.     long    ks_limit;    /* most that are allowed to exist */
  186.     long    ks_size;    /* sizes of this thing that are allocated */
  187.     long    ks_spare;
  188. };
  189.  
  190. /*
  191.  * Array of descriptors that describe the contents of each page
  192.  */
  193. struct kmemusage {
  194.     short ku_indx;        /* bucket index */
  195.     union {
  196.         u_short freecnt;/* for small allocations, free pieces in page */
  197.         u_short pagecnt;/* for large allocations, pages alloced */
  198.     } ku_un;
  199. };
  200. #define ku_freecnt ku_un.freecnt
  201. #define ku_pagecnt ku_un.pagecnt
  202.  
  203. /*
  204.  * Set of buckets for each size of memory block that is retained
  205.  */
  206. struct kmembuckets {
  207.     caddr_t kb_next;    /* list of free blocks */
  208.     caddr_t kb_last;    /* last free block */
  209.     long    kb_calls;    /* total calls to allocate this size */
  210.     long    kb_total;    /* total number of blocks allocated */
  211.     long    kb_totalfree;    /* # of free elements in this bucket */
  212.     long    kb_elmpercl;    /* # of elements in this sized allocation */
  213.     long    kb_highwat;    /* high water mark */
  214.     long    kb_couldfree;    /* over high water mark and could free */
  215. };
  216.  
  217. #ifdef KERNEL
  218. #define    MINALLOCSIZE    (1 << MINBUCKET)
  219. #define BUCKETINDX(size) \
  220.     (size) <= (MINALLOCSIZE * 128) \
  221.         ? (size) <= (MINALLOCSIZE * 8) \
  222.             ? (size) <= (MINALLOCSIZE * 2) \
  223.                 ? (size) <= (MINALLOCSIZE * 1) \
  224.                     ? (MINBUCKET + 0) \
  225.                     : (MINBUCKET + 1) \
  226.                 : (size) <= (MINALLOCSIZE * 4) \
  227.                     ? (MINBUCKET + 2) \
  228.                     : (MINBUCKET + 3) \
  229.             : (size) <= (MINALLOCSIZE* 32) \
  230.                 ? (size) <= (MINALLOCSIZE * 16) \
  231.                     ? (MINBUCKET + 4) \
  232.                     : (MINBUCKET + 5) \
  233.                 : (size) <= (MINALLOCSIZE * 64) \
  234.                     ? (MINBUCKET + 6) \
  235.                     : (MINBUCKET + 7) \
  236.         : (size) <= (MINALLOCSIZE * 2048) \
  237.             ? (size) <= (MINALLOCSIZE * 512) \
  238.                 ? (size) <= (MINALLOCSIZE * 256) \
  239.                     ? (MINBUCKET + 8) \
  240.                     : (MINBUCKET + 9) \
  241.                 : (size) <= (MINALLOCSIZE * 1024) \
  242.                     ? (MINBUCKET + 10) \
  243.                     : (MINBUCKET + 11) \
  244.             : (size) <= (MINALLOCSIZE * 8192) \
  245.                 ? (size) <= (MINALLOCSIZE * 4096) \
  246.                     ? (MINBUCKET + 12) \
  247.                     : (MINBUCKET + 13) \
  248.                 : (size) <= (MINALLOCSIZE * 16384) \
  249.                     ? (MINBUCKET + 14) \
  250.                     : (MINBUCKET + 15)
  251.  
  252. /*
  253.  * Turn virtual addresses into kmem map indicies
  254.  */
  255. #define kmemxtob(alloc)    (kmembase + (alloc) * NBPG)
  256. #define btokmemx(addr)    (((caddr_t)(addr) - kmembase) / NBPG)
  257. #define btokup(addr)    (&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT])
  258.  
  259. /*
  260.  * Macro versions for the usual cases of malloc/free
  261.  */
  262. #if defined(KMEMSTATS) || defined(DIAGNOSTIC)
  263. #define    MALLOC(space, cast, size, type, flags) \
  264.     (space) = (cast)malloc((u_long)(size), type, flags)
  265. #define FREE(addr, type) free((caddr_t)(addr), type)
  266.  
  267. #else /* do not collect statistics */
  268. #define    MALLOC(space, cast, size, type, flags) { \
  269.     register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
  270.     long s = splimp(); \
  271.     if (kbp->kb_next == NULL) { \
  272.         (space) = (cast)malloc((u_long)(size), type, flags); \
  273.     } else { \
  274.         (space) = (cast)kbp->kb_next; \
  275.         kbp->kb_next = *(caddr_t *)(space); \
  276.     } \
  277.     splx(s); \
  278. }
  279.  
  280. #define FREE(addr, type) { \
  281.     register struct kmembuckets *kbp; \
  282.     register struct kmemusage *kup = btokup(addr); \
  283.     long s = splimp(); \
  284.     if (1 << kup->ku_indx > MAXALLOCSAVE) { \
  285.         free((caddr_t)(addr), type); \
  286.     } else { \
  287.         kbp = &bucket[kup->ku_indx]; \
  288.         if (kbp->kb_next == NULL) \
  289.             kbp->kb_next = (caddr_t)(addr); \
  290.         else \
  291.             *(caddr_t *)(kbp->kb_last) = (caddr_t)(addr); \
  292.         *(caddr_t *)(addr) = NULL; \
  293.         kbp->kb_last = (caddr_t)(addr); \
  294.     } \
  295.     splx(s); \
  296. }
  297. #endif /* do not collect statistics */
  298.  
  299. extern struct kmemstats kmemstats[];
  300. extern struct kmemusage *kmemusage;
  301. extern char *kmembase;
  302. extern struct kmembuckets bucket[];
  303. extern void *malloc __P((unsigned long size, int type, int flags));
  304. extern void free __P((void *addr, int type));
  305. #endif /* KERNEL */
  306. #endif /* !_SYS_MALLOC_H_ */
  307.